/**
 * Created by lxy_920912 on 16/8/10.
 */
var editSubAttr = {
    coding:{'attr_name':'属性名称','attr_value':'属性值','attr_text':'显示名称','master_name':'主属性名称'},
    init:function () {

        var btn = document.getElementById('submitNewSubAttr');

        btn.onclick = function () {

            var subAttrNo = document.getElementById('attr_id');

            var form = document.getElementById('newSubAttr');
            var oData =document.getElementsByClassName('subinput1');
            var data = "";

            for(var i = 0;i < oData.length;i++){
                data+="'"+oData[i].id+"':'"+oData[i].value+"',";
            }
            console.log(data);
            data = "{"+data+"}";

            //添加到 数据库,通过属性名字查询新插入的数据
            data = eval("("+data+")");
            data['master_name']=data['parent_attr'];
            for(var i in editSubAttr.coding){
                console.log(i+":"+data[i]);
                if(data[i] ==''){
                    alert(editSubAttr.coding[i]+"不能为空!");
                    return;
                }
            }
            delete  data.attr_id;
            data['master_name'] = data['parent_attr'];
            delete  data.parent_attr;
            if(subAttrNo.value == -1){
                //新添加的数据
               editSubAttr.addNewSub(data);
                // 并返回子属性列表
            }else{
                //更新数据
                //查询更新后的数据
                var master_name = data.master_name;
                delete data.master_name;
                editSubAttr.editSubAttr(data,master_name);
            }
        }
        btn = document.getElementById('subCancel');
        btn.onclick = function () {
            indexModel.showDiv('subAttrListDetail');
        }

    },
    addNewSub:function (subAttr) {
        $.ajax({
            type:"get",
            url: indexModel.url+"detail/add",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            data :subAttr,
            success : function(json) {
                //alert(json);
                if(json=="0"){
                    alert("插入成功");
                    var oform = document.getElementById('newSubAttr');
                    oform.reset();
                    var master_name = subAttr['master_name']
                    subAttrListModel.showSubList(master_name);

                }else if(json=="1"){
                    alert('插入失败:子属性名字称冲突');
                    return;
                    //indexModel.showDiv('subAttrEditDetail');
                }else{
                    alert('插入失败:子属性名字称不能为空!!!!!');
                    console.log("json:"+json);
                    return;
                    //indexModel.showDiv('subAttrEditDetail');
                }
            },
            error: function() {
                console.log("tbl_getAttackSource没有值!");
            }
        });

    },
    editSubAttr:function (subAttr,master_name) {
        $.ajax({
            type:"get",
            url: indexModel.url+"detail/edit",
            dataType: "jsonp",
            jsonp: "callback",
            jsonpCallback:"data1",
            data :subAttr,
            success : function(json) {
                //alert(json);
                if(json=="0"){
                    alert("更新成功");
                    subAttrListModel.showSubList(master_name);
                }else if(json=="1"){
                    alert('插入失败:子属性名字称冲突');
                    indexModel.showDiv('mainAttrEditDetail');
                }else{
                    alert('插入失败:子属性名字称不能为空');
                    console.log('json :'+json);
                    return ;

                    indexModel.showDiv('mainAttrEditDetail');
                }
            },
            error: function() {
                console.log("tbl_getAttackSource没有值!");
            }
        });
    }

}